﻿using CoreShop.Library;
using Sunny.UI;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using static Sunny.UI.UITextBox;

namespace CoreShop.App
{
    public partial class UINumKeybord : UIUserControl
    {
        public UINumKeybord()
        {
            InitializeComponent();
        }

        public UITextBox edit;
        private UIEditType EditType;
        private int DecimalPlaces = 2;

        public void SetTextBox(UITextBox box, UIEditType textType, int decimalPlaces = 2)
        {
            edit = box;
            EditType = textType;
            DecimalPlaces = decimalPlaces;
            if (textType == UIEditType.Double && decimalPlaces == 0)
                EditType = UIEditType.Integer;
        }

        public static int WM_CHAR = 256;
        public static int WM_SETSEL = 173;
        public static int WM_KEYDOWN = 0x0100;
        public static int WM_KEYUP = 0x0101;

        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        protected void DoValueChanged(object sender, int value, string text)
        {
            if (edit == null) return;
            int start = edit.SelectionStart;
            int len = edit.SelectionLength;
            edit.Focus();
            edit.SelectionStart = start;
            edit.Select(start, len);

            string remainText = edit.Text;
            if (len > 0)
            {
                remainText = remainText.Left(start) + remainText.Middle(start + len, remainText.Length);
            }

            if (EditType == UIEditType.Integer || EditType == UIEditType.Double)
            {
                if (text == "-")
                {
                    if (start == 0)
                    {
                        if (remainText.Contains("-")) return;
                    }
                    else
                    {
                        return;
                    }
                }
            }

            if (EditType == UIEditType.Integer)
            {
                if (text == ".") return;
                if (edit.Text.Replace("-", "").Length >= 9 && len == 0 && value >= 48 && value <= 57) return;
                if (text == "0" && start == 0) return;
            }

            if (EditType == UIEditType.Double)
            {
                if (text == ".")
                {
                    if (len == 0)
                    {
                        if (edit.Text.Contains(".")) return;
                    }
                    else
                    {
                        if (!edit.Text.Middle(start, len).Contains(".")) return;
                    }

                    if (start == 0)
                    {
                        return;
                    }
                    else if (start == 1)
                    {
                        if (edit.Text.Left(1) == "-") return;
                    }
                }

                if (text == "0")
                {
                    if (edit.Text.Left(start) == "0" && start == 1) return;
                    if (edit.Text.Left(start) == "-0" && start == 2) return;
                    if (start == 0 && remainText.IsValid()) return;
                }

                int index = edit.Text.IndexOf(".");
                if (index > 0 && len == 0 && value >= 48 && value <= 57)
                {
                    index = edit.Text.Length - index;
                    if (index >= DecimalPlaces + 1 && start > edit.Text.IndexOf(".")) return;
                }
            }

            if (len > 0)
            {
                SendKeys.SendWait("{BACKSPACE}");
                if (text == "{BACKSPACE}") return;
                SendKeys.SendWait(text);
            }
            else
            {
                SendKeys.SendWait(text);
            }

            SendKeys.Flush();
            //Console.WriteLine(edit.Text);
        }

        private void uiSymbolButton1_Click(object sender, System.EventArgs e)
        {
            if (edit == null)
            {
                UIMessageTip.ShowError("请选择编辑框");
                return;
            }

            UISymbolButton btn = (UISymbolButton)sender;
            DoValueChanged(this, btn.Tag.ToString().ToInt(), btn.TagString);
        }

        private void uiSymbolButton15_Click(object sender, System.EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            DoValueChanged(this, btn.Tag.ToString().ToInt(), btn.TagString);
            DoValueChanged(this, btn.Tag.ToString().ToInt(), btn.TagString);
        }

        public delegate void OnOKButtonClick(object sender, string text, int intvalue, double doublevalue);

        public event OnOKButtonClick OKButtonClick;

        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            if (EditType == UIEditType.String)
            {
                OKButtonClick?.Invoke(edit, edit.Text, 0, 0);
            }

            if (EditType == UIEditType.Integer)
            {

                OKButtonClick?.Invoke(edit, edit.Text, CommFunc.ConvertToInt32(edit.Text), 0);
            }

            if (EditType == UIEditType.Double)
            {

                OKButtonClick?.Invoke(edit, edit.Text, 0, CommFunc.ConvertToDouble(edit.Text));
            }
        }
    }
}
